package com.tale.springbootatm.common.util;

import com.tale.springbootatm.common.constant.StatusCode;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;

/**
 * author: 蔡奇峰
 * date: 2020/4/11 15:05
 * @Version V1.0
 **/
@Data
public class R {
    private int code;

    private String message;

    private boolean success;

    private Map<String, Object> data = new HashMap<String, Object>();


    /**
     * 操作成功
     * @return
     */
    public static R success(){
        R r = new R();
        r.setCode(StatusCode.SUCCESS);
        r.setMessage("操作成功");
        r.setSuccess(true);

        return r;
    }

    /**
     * 操作失败
     * @return
     */
    public static R fail(){
        R r = new R();
        r.setCode(StatusCode.FAIL);
        r.setMessage("操作失败");
        r.setSuccess(false);

        return r;
    }

    public R message(String msg){
        this.setMessage(msg);

        return this;
    }


    public R data(String key, Object value){
        this.data.put(key, value);

        return this;
    }

    public R data(Map<String, Object> map){
        this.setData(map);

        return this;
    }

    public R code(int code){
        this.setCode(code);

        return this;
    }


    public R() {
    }

    // 全局异常使用
    public R(int code, String msg){
        this.setCode(code);

        this.setMessage(msg);
    }
}
